package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author faiz
 * @since 2021-05-13
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //查询一天的注册人数
    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegisterDay(day);
    }

    //根据openid来判断用户是否存在
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        UcenterMember member = getOne(wrapper);
        return member;
    }

    //登录方法
    @Override
    public String login(UcenterMember member) {
        //获取登录手机号
        String mail = member.getMail();
        String password = member.getPassword();
        //手机号和密码非空判定
        if (StringUtils.isEmpty(mail) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"手机号或密码为空!");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mail", mail);
        UcenterMember ucenterMember = getOne(wrapper);
        if (ucenterMember == null) {
            throw new GuliException(20001, "登录失败");
        }
        //判断密码
        //存储到数据库的密码是密文
        //把输入的密码进行加密，在和数据库密码进行比较
        //加密方式一般使用MD5(非对称加密，不可解密)
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
            throw new GuliException(20001, "登录失败");
        }
        //判断用户是否禁用
        if (ucenterMember.getIsDisabled()) {
            throw new GuliException(20001, "登录失败");
        }
        //登录成功
        //生成一个token字符串
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());

        return token;
    }
    //注册方法
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode();
        //获取手机号
        String mail = registerVo.getMail();
        //昵称
        String nickname = registerVo.getNickname();
        //用户密码
        String password = registerVo.getPassword();

        if (StringUtils.isEmpty(mail) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登录失败！");
        }

        //判断邮箱验证码是否正确
        //获取redis中的验证码
        String redisCode = redisTemplate.opsForValue().get(mail);
        if (!code.equals(redisCode)) {
            throw new GuliException(20001, "验证码错误或失效，请重新申请验证码！");
        }

        //判断邮箱号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mail", mail);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new GuliException(20001, "注册失败，该账号已被申请！");
        }

        //数据库中添加数据
        UcenterMember member = new UcenterMember();
        member.setMail(mail);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        save(member);
    }
}
